/* 
错误写法：每次调用 Symbol 函数得到的符号永远不相等，无论符号名是否相同
*/
const hero = {
    attack: 30,
    hp: 300,
    defence: 10,
    gongji() { //攻击
        //伤害：攻击力*随机数（0.8~1.1)
        const dmg = this.attack * this[Symbol("Random")](0.8, 1.1);
        console.log(dmg);
    },
    [Symbol("Random")](min, max) { //根据最小值和最大值产生一个随机数
        return Math.random() * (max - min) + min;
    }
}

/* 
正确做法：匿名函数自执行 + 闭包getRandom
*/

const hero1 = (() => {
    const getRandom = Symbol();
    return {
        attack: 30,
        hp: 300,
        defence: 10,
        gongji() { //攻击
            //伤害：攻击力*随机数（0.8~1.1)
            const dmg = this.attack * this[getRandom](0.8, 1.1);
            console.log(dmg);
        },
        [getRandom](min, max) { //根据最小值和最大值产生一个随机数
            return Math.random() * (max - min) + min;
        }
    }
})()

console.log(hero1);




const Hero = (() => {
    const getRandom = Symbol();

    return class {
        constructor(attack, hp, defence) {
            this.attack = attack;
            this.hp = hp;
            this.defence = defence;
        }

        gongji() {
            //伤害：攻击力*随机数（0.8~1.1)
            const dmg = this.attack * this[getRandom](0.8, 1.1);
            console.log(dmg);
        }

        [getRandom](min, max) { //根据最小值和最大值产生一个随机数
            return Math.random() * (max - min) + min;
        }
    }
})();

const h = new Hero(3, 6, 3);
console.log(h); //类里面的函数，都在原型链上
